package com.ulic.customerManApplication.commonmodule.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseDao;
import com.ulic.customerManApplication.commonmodule.bo.Organization;
import com.ulic.customerManApplication.commonmodule.dao.OrganizationDao;
import com.ulic.customerManApplication.usermodule.bo.User;

@Component("organizationDao")
public class OrganizationDaoImpl extends BaseDao implements OrganizationDao {

	 Session session = null;

	@SuppressWarnings("unchecked")
	@Override
	public List<Organization> getOrganizations() {
		// TODO Auto-generated method stub
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Organization.class);
		List list = cri.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Organization getOrganizationByFullName(String fullName) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Organization.class);
		Organization organization = null;
		if(fullName!=null&&fullName.length()!=0){
			List list = cri.add(Restrictions.eq("abbr_name", fullName.trim())).list();
			if(list.size()>0){
				organization = (Organization)list.get(0);
			}
		}
		return organization;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Organization> getOrganizations(String roleName,User user) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Organization.class); 
		cri.add(Restrictions.eq("um_organ_id", user.getOrganization().getUm_organ_id()));
		List list = cri.list();
		return list;
	}

	/**
	 * Description: 获得父机构
	 * @Version1.0 
	 * 2012-9-7 下午05:12:36 李求智 (liqz@huilan.com) created
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Organization getFinalOrganization() {
		session  = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Organization.class);
		List<Organization> list = cri.add(Restrictions.isNull("parent_id")).list();
		Organization organization = null;
		if(list.size()>0){
			organization = list.get(0);
		}
		return organization;
	}

	/**
	 * Description: 根据parentid获得子机构
	 * @Version1.0 
	 * 2012-9-7 下午05:12:34 李求智 (liqz@huilan.com) created
	 * @param parentId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Organization> findOrganizationByParentId(String parentId) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Organization.class);
		List<Organization> list = null;
		if(parentId.equals(getFinalOrganization().getUm_organ_id())){
			list = cri.add(Restrictions.eq("parent_id", parentId)).add(Restrictions.eq("status", "Y")).add(Restrictions.like("abbr_name","%合众人寿%")).addOrder(Order.asc("um_organ_id")).list();
		}else{
			list = cri.add(Restrictions.eq("parent_id", parentId)).add(Restrictions.eq("status", "Y")).addOrder(Order.asc("um_organ_id")).list();
		}
		return list;
	}

	/**
	 * Description: 判断机构是否包含用户
	 * @Version1.0 
	 * 2012-9-7 下午05:16:28 李求智 (liqz@huilan.com) created
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public boolean organIncludeUser(String id) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(User.class);
		List<User> list = cri.add(Restrictions.eq("organization.um_organ_id", id)).list();
		return list!=null&&list.size()>0;
	}

	/**
	 * Description: 判断是否是中支
	 * @Version1.0 
	 * 2012-9-10 下午03:21:11 李求智 (liqz@huilan.com) created
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public boolean organIsExpensed(String id,String parentid) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Organization.class);
		Organization organ= (Organization)cri.add(Restrictions.eq("um_organ_id", id)).add(Restrictions.eq("parent_id", parentid)).uniqueResult();
		return organ!=null;
	}

	/**
	 * Description: 查询用户所在的分公司
	 * @Version1.0 
	 * 2012-9-12 下午05:03:00 李求智 (liqz@huilan.com) created
	 * @param userOid
	 * @return
	 */
	@Override
	public Organization getBranchByUserOid(String organOid) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Organization.class);
		Organization organ = (Organization)cri.add(Restrictions.eq("um_organ_id", organOid)).uniqueResult();
		return getBranch(organ);
	}
	
	/**
	 * Description: 递归查询分公司
	 * @Version1.0 
	 * 2012-9-12 下午05:08:44 李求智 (liqz@huilan.com) created
	 * @param organOid
	 * @return
	 */
	public Organization getBranch(String organOid){
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Organization.class);
		Organization organ = (Organization)cri.add(Restrictions.eq("um_organ_id", organOid)).uniqueResult();
		if(organ.getParent_id()==null||organ.getParent_id()==""){
			return organ;
		}else{
			return getBranch(organ.getParent_id());
		}
	}

	/**
	 * Description: 查询用户所在的中支
	 * @Version1.0 
	 * 2012-9-12 下午05:03:16 李求智 (liqz@huilan.com) created
	 * @param userOid
	 * @return
	 */
	@Override
	public Organization getExpensedByUserOid(String organOid,String organOid1) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Organization.class);
		Organization organ = (Organization)cri.add(Restrictions.eq("um_organ_id", organOid)).uniqueResult();
		if(organ.getParent_id().equals(organOid1)){
			return organ;
		}else{
			return getExpensed(organ.getParent_id(),organOid1);
		}
	}

	/**
	 * Description: 递归查询分公司
	 * @Version1.0 
	 * 2012-9-12 下午05:08:44 李求智 (liqz@huilan.com) created
	 * @param organOid
	 * @return
	 */
	public Organization getExpensed(String organOid,String organOid1){
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Organization.class);
		Organization organ = (Organization)cri.add(Restrictions.eq("um_organ_id", organOid)).uniqueResult();
		if(organ.getParent_id().equals(organOid1)){
			return organ;
		}else{
			return getExpensed(organ.getParent_id(),organOid1);
		}
	}

	@Override
	public Organization getOrganizationByUMID(String umOrganId) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Organization.class);
		Organization organ = (Organization)cri.add(Restrictions.eq("um_organ_id", umOrganId)).uniqueResult();
	    return organ;
	}
	
	
	public Organization getBranch(Organization organ){
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Organization.class);
		if(organ.getParent_id()!=null ||organ.getParent_id()!=""){
			if(organ.getAbbr_name().startsWith("合众人寿")){
				return organ;
			}else{
				organ = (Organization)cri.add(Restrictions.eq("um_organ_id", organ.getParent_id())).uniqueResult();
				return getBranch(organ);
			}
		}else{
			return organ;
		}
		
	}

	@Override
	public List<Organization> getOrgansBySubCityId(String organOid) {
		List<Organization> list = new ArrayList<Organization>();
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(Organization.class);
		list = cri.add(Restrictions.eq("parent_id", organOid)).list();
		return list;
	}
}
